//
//  HQShopUploadViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/12.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQShopUploadViewController.h"

@interface HQShopUploadViewController ()
{
    __weak IBOutlet NSLayoutConstraint *_bottomConstraint;
}
@end

@implementation HQShopUploadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if([self.pushType isEqualToString:@"EDIT"]){
        NSLog(@"编辑的数据是: %@",_dataInfo);
        self.title = @"编辑产品信息";
        // 图片
        [_tuPian setImageWithURL:[NSURL URLWithString:self.dataInfo[@"w_img"]]];
        // 产品编号
        _anliNo.text = self.dataInfo[@"w_num"];
        // 产品数量
        _shuLiang.text = [NSString stringWithFormat:@"%@",self.dataInfo[@"w_kucun"]];
        // 零售价格
        _lingShouJiaGe.text = [NSString stringWithFormat:@"%@",self.dataInfo[@"w_price"]];
        // 产品风格
        _fengGe.text = self.dataInfo[@"w_style"];
        // 产品成分
        _chengFen.text = self.dataInfo[@"w_element"];
        // 产品理念
        _liNian.text = self.dataInfo[@"w_concept"];
        // 产品风格
        _fengGe.text = self.dataInfo[@"w_style"];
        _w_style = self.dataInfo[@"w_style"];
        _w_style_id = [NSString stringWithFormat:@"%@",self.dataInfo[@"w_style_id"]];
    }else if([self.pushType isEqualToString:@"ADD"]){
        self.title = @"产品上传";
    }
    [self addKeyBoardObserver];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    _bottomConstraint.constant =keyboardRect.size.height;
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    _bottomConstraint.constant = 0;
}

/**
 拍照上传图片
 */
-(void)paizhao{
    NSLog(@"拍照");
    [[ImagePickerTool sharedInstance] pickImageWithType:WLImagePickerTypeCamera context:self finishBlock:^(BOOL isSuccess, UIImage *image) {
        if (isSuccess) {
            /** 上传图片*/
            _tuPian.image = image;
        } else {
            [self tostView:@"调取本地相册失败"];
        }
    }];
}

/**
 相册选择图片
 */
-(void)xiangce{
    NSLog(@"照片");
    [[ImagePickerTool sharedInstance] pickImageWithType:WLImagePickerTypeLocal context:self finishBlock:^(BOOL isSuccess, UIImage *image) {
        if (isSuccess) {
            /** 上传图片*/
            _tuPian.image = image;
        } else {
            [self tostView:@"调取本地相册失败"];
        }
    }];
}

/**
 保存当前数据
 
 @param sender 按钮
 */
- (IBAction)saveDataItem:(id)sender {
    if(_tuPian.image == nil){
        [self tostView:@"请上传图片"];
        return;
    }else if([self isInputEmpty:_anliNo.text]){
        [self tostView:@"请输入产品编号"];
        return;
    }else if([self isInputEmpty:_shuLiang.text]){
        [self tostView:@"请输入产品数量"];
        return;
    }else if([self isInputEmpty:_lingShouJiaGe.text]){
        [self tostView:@"请输入零售价格"];
        return;
    }else if([self isInputEmpty:_fengGe.text]){
        [self tostView:@"请选择产品风格"];
        return;
    }else if([self isInputEmpty:_chengFen.text]){
        [self tostView:@"请选择产品成分"];
        return;
    }else if([self isInputEmpty:_liNian.text]){
        [self tostView:@"请输入产品理念"];
        return;
    }
    [self showHudText:@"上传图片..."];
    HQNetWork *hqnetwork = [[HQNetWork alloc]init];
    [hqnetwork httpUploadImages:_tuPian.image URL:[HQMethod uploadface] param:nil suc:^(NSDictionary *suc) {
        [self hiddnHud];
        if([suc[kStatus]integerValue] == 1){
            self.imgurl = [[suc objectForKey:@"data"] objectForKey:@"up_imgurl"];
            if([self.pushType isEqualToString:@"EDIT"]){
                [self editAnli];
            }else if([self.pushType isEqualToString:@"ADD"]){
                [self uploadAnli];
            }
        }else{
            [self tostView:suc[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 保存案例数据
 */
-(void)editAnli{
    [self showHudText:@"修改中..."];
    [HQNetWork requetParam:[HQParam editWorks:[UserModelManager userID] w_id:self.dataInfo[@"w_id"] w_num:_anliNo.text w_kucun:_shuLiang.text w_price:_lingShouJiaGe.text w_style:_w_style w_style_id:_w_style_id w_element:_chengFen.text w_concept:_liNian.text w_img:self.imgurl] withMethod:[HQMethod editWorks] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            [self tostView:@"修改成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.delegate reloadShopData];
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 保存案例数据
 */
-(void)uploadAnli{
    [self showHudText:@"上传中..."];
    [HQNetWork requetParam:[HQParam addWorks:[UserModelManager userID] w_num:_anliNo.text w_kucun:_shuLiang.text w_price:_lingShouJiaGe.text w_style:_w_style w_style_id:_w_style_id w_element:_chengFen.text w_concept:_liNian.text w_img:self.imgurl] withMethod:[HQMethod worksAddWorks] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            [self tostView:@"店内上传成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.delegate reloadShopData];
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 选择图片上传
 
 @param sender 按钮
 */
- (IBAction)selectImage:(id)sender {
    [self.view endEditing:YES];
    NSArray *objs = [[NSBundle mainBundle]loadNibNamed:@"HQAddPhotoView" owner:nil options:nil];
    HQAddPhotoView *photoView = objs[0];
    photoView.delegate = self;
    photoView.frame = [UIScreen mainScreen].bounds;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:photoView];
}

/**
 产品风格
 
 @param sender 按钮
 */
- (IBAction)chanPinFengGe:(id)sender {
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam getsearchdata:[UserModelManager userID]] withMethod:[HQMethod getsearchdata] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            _fengGeArray = [[NSMutableArray alloc]initWithArray:sucData[@"data"][@"stylelists"]];
            [_fengGeArray removeObjectAtIndex:0];
            NSMutableArray *m = [[NSMutableArray alloc]init];
            for(NSDictionary *dict in _fengGeArray){
                [m addObject:[NSString stringWithFormat:@"%@",dict[@"tse_name"]]];
            }
            ZHPickView *zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
            [zhPickerView setPickeTitle:@"产品风格"];
            [zhPickerView selectObj:_fengGe.text];
            zhPickerView.delegate = self;
            zhPickerView.tag = 2001;
            [zhPickerView show];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 选择代理方法

 @param pickView 弹出视图
 @param resultString 选择结果
 */
-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    if(pickView.tag == 2001){
        for(NSDictionary *dict in _fengGeArray){
            if([dict[@"tse_name"] isEqualToString:resultString]){
                _w_style_id = dict[@"tse_id"];
                _w_style = dict[@"tse_name"];
                break;
            }
        }
        _fengGe.text = resultString;
    }
}

@end
